package com.zx.mobile.controller;

import java.net.URLDecoder;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.zx.map.service.ConfService;
import com.zx.map.service.OrdLogService;
import com.zx.map.service.RiskControlService;
import com.zx.map.service.model.OrdLogModel;
import com.zx.map.service.model.RiskControlModel;
import com.zx.map.utils.CommonResponseUtils;
import com.zx.map.utils.SUConstants;
import com.zx.mobile.MobileCommonHelper;
import com.zx.mobile.domain.MobileUserDomain;
import com.zx.mobile.service.proxy.IncomePayProxy;
import com.zx.mobile.utils.WebUtils;
import com.zx.platform.utils.AlipayUtil;
import com.zx.platform.utils.PmpUtils;

/**
 * Created by IntelliJ IDEA. User: Administrator Date: 15-7-5 Time: 下午8:09 To
 * change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/mobile/index")
public class MobileIncomeController {
	/**
	 * log
	 */
	private static Logger log = Logger.getLogger(MobileIncomeController.class);
	@Autowired
	private ConfService confService;
	@Autowired
	private OrdLogService ordLogService;
	@Autowired
	private RiskControlService riskControlService;

	/**
	 * 
	 * @param incomeAmt
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping("/incomeGetPay")
	public String incomeGetPay(String incomeAmt, Model model, HttpServletRequest request) {
		model.addAttribute("incomeAmt", incomeAmt);
		return "/jsp/mobile/incomePayType";
	}

	@RequestMapping("/conf")
	@ResponseBody
	public Map conf(HttpServletRequest request) {
		try {

			return CommonResponseUtils.successResponse(confService.list());
		} catch (Throwable e) {
			e.printStackTrace();
			return CommonResponseUtils.failureResponse(e.getMessage());
		}

	}

	/**
	 * 获取订单二维码
	 *
	 * @param incomeAmt
	 * @param model
	 * @param request
	 * @return
	 */
	
	private static final String PLANT_NAME="宝树科技合作商户";
	
	
	@RequestMapping("/incomeGetPayForPayType")
	public String incomeGetPayForPayType(String incomeAmt, String payChannel, Model model, HttpServletRequest request) {
		try {
			MobileUserDomain user = WebUtils.queryCurrentLoginInfo(request);
			// 事中风控 对交易金额进行限制
			RiskControlModel riskControlModel = riskControlService.bussinessAmtRiskControl(SUConstants.TRANSTYPE_INCOME,
					incomeAmt, user.getMerchantInfoDetailModel());
			if (MobileCommonHelper.RISKPASSCODE.equals(riskControlModel.getRiskCode())) {
				String orderId = PmpUtils.getPayOrderNum();
				Map map = IncomePayProxy.getOnlineIncomePay(incomeAmt, orderId, payChannel, null);
				if (null != map && SUConstants.CODE_SUCCESS_VALUE.equals((String) (map.get(SUConstants.CODE_KEY)))) {
					// 保存订单信息
					OrdLogModel ordLogModel = IncomePayProxy.transformToOrdLog(user, payChannel,
							SUConstants.TRANSTYPE_INCOME);
					ordLogModel.setPaymentAmt(Double.valueOf(incomeAmt));
					ordLogModel.setOrdId(orderId);
					ordLogService.add(ordLogModel);
					
					
					
				
					
					// 返回二维码信息
					Map info = (Map) map.get("info");
					
					if (SUConstants.paymentTypeWX.equals(payChannel)) {
			        } else {
			        	String url =AlipayUtil.payOrder(orderId, incomeAmt, PLANT_NAME,user.getMerchantInfoDetailModel().getMerchantId() );
			        	info.put("codeUrl",url);
			        	info.put("codeImgUrl","downloadQrURL?url="+url);
			        	
			        }
					
					
					model.addAttribute("codeImgUrl", info.get("codeImgUrl"));
					model.addAttribute("codeUrl", info.get("codeUrl"));
					model.addAttribute("amount", incomeAmt);
					model.addAttribute("orderId", orderId);
					model.addAttribute("payChannel", payChannel);
					return "/jsp/mobile/incomesecond";
				} else {
					model.addAttribute("errorMsg", "获取二维码异常");
					log.error("获取二维码异常");
					return "/jsp/mobile/error";
				}
			} else {
				model.addAttribute("errorMsg", riskControlModel.getRiskMsg());
				log.error("未通过系统风控:" + riskControlModel.getRiskMsg());
				return "/jsp/mobile/error";
			}
		} catch (Exception e) {
			model.addAttribute("errorMsg", "获取二维码异常");
			log.error("获取二维码异常", e);
			return "/jsp/mobile/error";
		}
	}

	/**
	 * 直接充值
	 *
	 * @param incomeAmt
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping("/rechargeForPayType")
	public String rechargeForPayType(String incomeAmt, String payChannel, Model model, HttpServletRequest request) {
		try {
			MobileUserDomain user = WebUtils.queryCurrentLoginInfo(request);
			String orderId = PmpUtils.getRechargeOrderNum();
			Map map = IncomePayProxy.getRechargePay(incomeAmt, orderId, payChannel, null);
			log.info("二维码接口返回信息:" + ToStringBuilder.reflectionToString(map));
			if (null != map && SUConstants.CODE_SUCCESS_VALUE.equals((String) (map.get(SUConstants.CODE_KEY)))) {
				// 保存订单信息
				OrdLogModel ordLogModel = IncomePayProxy.transformToOrdLog(user, payChannel,
						SUConstants.TRANSTYPE_RECHARGE);
				ordLogModel.setPaymentAmt(Double.valueOf(incomeAmt));
				ordLogModel.setOrdId(orderId);
				ordLogService.add(ordLogModel);
				// 返回二维码信息
				Map info = (Map) map.get("info");
				String url = URLDecoder.decode((String) info.get("codeUrl"), "utf-8");
				return "redirect:" + url;
			}
			model.addAttribute("errorMsg", "支付信息获取失败");
			return "/jsp/mobile/offline/error";
		} catch (Exception e) {
			model.addAttribute("errorMsg", "支付信息获取失败");
			log.error("支付信息获取失败", e);
			return "/jsp/mobile/offline/error";
		}

	}

	/**
	 * 支付限额表
	 *
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping("/payamtlimit")
	public String payamtlimit(Model model, HttpServletRequest request) {
		try {
			return "/jsp/mobile/payamtlimit";
		} catch (Exception e) {
			model.addAttribute("errorMsg", "系统异常");
			log.error("系统异常", e);
			return "/jsp/mobile/error";
		}
	}

	/**
	 * 扫码支付
	 *
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping("/incomefirst")
	public String incomefirst(Model model, HttpServletRequest request) {
		return "/jsp/mobile/incomefirst";
	}

	/**
	 * 交易签名页面
	 *
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping("/incomesign")
	public String incomesign(Model model, HttpServletRequest request) {
		return "/jsp/mobile/incomesign";
	}

	/**
	 * 交易失败页面
	 *
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping("/incomefail")
	public String incomefail(Model model, HttpServletRequest request) {
		return "/jsp/mobile/incomefail";
	}

	/**
	 * 交易成功页面
	 *
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping("/incomesuccess")
	public String incomesuccess(Model model, HttpServletRequest request) {
		return "/jsp/mobile/incomesuccess";
	}
}
